package com.hopu.springbootdemo2.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 日志切面
 */
@Aspect
//@Component
public class SystemLogAspect {

    //定义日志对象
    private Logger logger = Logger.getLogger(SystemLogAspect.class);

    //定义切面
    @Pointcut("execution(* com.hopu.springbootdemo2.service.impl.*ServiceImpl.*(..))")
    public void pointcut(){}

    @Around("pointcut()")
    public Object aroundLog(ProceedingJoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        logger.debug("执行方法：" + name);
        //输出方法的参数
        Object[] args = joinPoint.getArgs();
        for(Object arg : args){
            logger.debug("参数：" + arg);
        }
        try {
            long start = System.currentTimeMillis();
            //调用原方法
            Object rs = joinPoint.proceed();
            long end = System.currentTimeMillis();
            logger.debug("返回值：" + rs);
            logger.debug("方法执行时间(毫秒):" + (end - start));
            return rs;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
